package test.temp;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import utils.SmallTool;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;

/**
 * @Author: gcr
 * @Date: 2023/2/11 16:17
 */
public class CheckPagePM {
    @SneakyThrows
    public static void main(String[] args) {
        // 用于检查错误页码 重新解析数据入库 sj2
        String dbNameMy = "spider_pubmed_vip_extend";
        String errorPMID = "error_pmid";
        Set<Integer> allPmid = new ConcurrentHashSet<>();
        int breakSize = 100000;
        SmallTool.printMemory();
        Db use3307 = Db.use(DSFactory.get("local_articleadd"));

        String allDate = "select max(id) as ids  from " + dbNameMy;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));
        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start1 = breakSize * (i - 1) + 1;
            int end1 = i * breakSize;
            SmallTool.printMess("开始页码++" + i + "++id++" + start1 + "结束id++++" + end1);
            String meSql = "select id,pmid,pages from " + dbNameMy + " where id between " + start1 + " and " + end1;
            List<Entity> queryMe = use3307.query(meSql);
            queryMe.parallelStream().forEach(pmDeatil -> {
                Integer id = pmDeatil.getInt("id");
                Integer pmid = pmDeatil.getInt("pmid");
                String pages = pmDeatil.getStr("pages");
                if (StringUtils.isNotEmpty(pages) && pages.contains("-")) {
                    String aw = pages.split("-")[0];
                    boolean numeric = StringUtils.isNumeric(aw);
                    if (numeric) {
                        Integer start = Integer.parseInt(pages.split("-")[0]);
                        Integer end = 999999999;
                        try {
                            end = Integer.parseInt(pages.split("-")[1]);
                        } catch (Exception e) {

                        }
                        if (end < start) {
//                            SmallTool.printMess("错误页码 进行重置++pages++" + pages);
                            allPmid.add(pmid);
                        }
                    }

                }

            });
        }
        allPmid.parallelStream().forEach(a -> {
            Entity ins = new Entity();
            ins.setTableName(errorPMID);
            ins.set("pmid", a);
            try {
                use3307.insert(ins);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        });


    }
}
